package me.alex.threadpooldemo;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * ================================================
 * Description:
 * <p>
 * Created by Alex on 2021/4/21
 * <p>
 * 页面内容介绍:
 * <p>
 * ================================================
 */
public class BlockingQueueActivity extends AppCompatActivity {
    /**
     * 阻塞队列容器
     * 学习文档:https://blog.csdn.net/qq_42135428/article/details/80285737
     */
    private LinkedBlockingQueue<Data> dataBlockingQueue = new LinkedBlockingQueue<>();
    /**
     * 处理Data对象的线程,可以是数据处理/数据发送/网络接口/串口发送等任何耗时或不耗时操作都可以
     */
    private Thread handlerThread1, handlerThread2;

    Button addData, showData;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blockingqueue);
        addData = findViewById(R.id.queue_add_data);
        showData = findViewById(R.id.queue_show_data);
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < 20; i++) {
                    int num = new Random().nextInt(20);//获取一个0-20随机数
                    dataBlockingQueue.add(new Data(num, new CountChangeListener() {
                        @Override
                        public void onSuccess(String result) {
                            Log.i("handleData success", result);
                        }

                        @Override
                        public void onFailed(String failedResult) {
                            Log.i("handleData onFailed", failedResult);
                        }
                    }));
                }
            }
        });

        for (int i = 0; i < 20; i++) {
            dataBlockingQueue.add(new Data(new Random().nextInt(20), new CountChangeListener() {
                @Override
                public void onSuccess(String result) {
                    Log.i("handleData success", result);
                }

                @Override
                public void onFailed(String failedResult) {
                    Log.i("handleData onFailed", failedResult);
                }
            }));
        }

        handlerThread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                handleData();
            }
        });
        handlerThread1.setDaemon(true);
        handlerThread1.start();

        handlerThread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                handleData2();
            }
        });
        handlerThread2.setDaemon(true);
        handlerThread2.start();

    }

    private void handleData() {
        Log.i("handleData", "handleData");
        while (true) {
            try {
                Log.i("handleData", "try");
                final Data data = dataBlockingQueue.take();//当dataBlockingQueue中没有就停止
                if (data.getCount() >= 10) {
                    data.setCount(data.getCount() - 3);
                    data.getListener().onSuccess("处理成功: 处理后 count = " + data.getCount());
                } else {
                    data.getListener().onFailed("count小于10,处理失败: count = " + data.getCount());
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showData.setText(String.valueOf(data.getCount()));
                    }
                });
                Thread.sleep(400);
            } catch (InterruptedException e) {
                Log.i("handleData", "catch");
                e.printStackTrace();
            }
        }
    }

    private void handleData2() {
        Log.e("handleData", "handleData");
        while (true) {
            try {
                Log.e("handleData", "try");
                final Data data = dataBlockingQueue.take();//当dataBlockingQueue中没有
                if (data.getCount() >= 10) {
                    data.setCount(data.getCount() - 3);
                    data.getListener().onSuccess("处理成功: 处理后 count = " + data.getCount());
                } else {
                    data.getListener().onFailed("count小于10,处理失败: count = " + data.getCount());
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showData.setText(String.valueOf(data.getCount()));
                    }
                });
                Thread.sleep(300);
            } catch (InterruptedException e) {
                Log.e("handleData", "catch");
                e.printStackTrace();
            }
        }
    }

}
